﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using YArchitech.Electric;
using YArchitech.LIB;
using libxl;

namespace HYElectricSetting
{
	public partial class CableManagerForm : System.Windows.Forms.Form, IElectricSettingItem
	{
		public CableManagerForm()
		{
			this.InitializeComponent();
			this.dataList = new List<CableData>();
			this.isEdited = false;
		}

		public bool IsEdited
		{
			get
			{
				return this.isEdited;
			}
			set
			{
				this.isEdited = value;
				HYElectricSettingMainForm hyelectricSettingMainForm = base.Tag as HYElectricSettingMainForm;
				bool flag = this.isEdited && hyelectricSettingMainForm != null;
				if (flag)
				{
					hyelectricSettingMainForm.ValidApplyButton();
				}
			}
		}

		public void SaveElectricSettingData()
		{
			this.SaveToDatabase();
		}

		private void CableManagerForm_Load(object sender, EventArgs e)
		{
			this.InitButtonImage();
			this.InitDataGridView();
		}

		private void InitButtonImage()
		{
			this.buttonImport.Image = YArchitech.Electric.Resources.GetImage("DataLoad16");
			this.buttonDelete.Image = YArchitech.Electric.Resources.GetImage("Delete20");
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.buttonImport, "导入");
			toolTip.SetToolTip(this.buttonDelete, "删除");
		}

		private void InitDataGridView()
		{
			this.dataList = CableDatabase.ThisDb.GetCableDatas();
			this.UpdateDataGridView(true);
		}

		private void UpdateDataGridView(bool flag)
		{
			if (flag)
			{
				this.dataGridView.Rows.Clear();
				foreach (CableData cableData in this.dataList)
				{
					DataGridViewRow dataGridViewRow = new DataGridViewRow();
					int num = this.dataGridView.Rows.Add(dataGridViewRow);
					dataGridViewRow = this.dataGridView.Rows[num];
					dataGridViewRow.Cells[0].Value = num + 1;
					dataGridViewRow.Cells[1].Value = cableData.CableName;
					dataGridViewRow.Cells[2].Value = cableData.CableModel;
					dataGridViewRow.Cells[3].Value = cableData.CableSpec;
					dataGridViewRow.Cells[4].Value = cableData.SystemType;
					dataGridViewRow.Cells[5].Value = cableData.OutDiameter;
					dataGridViewRow.Tag = cableData;
				}
			}
			else
			{
				this.dataList.Clear();
				foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
				{
					DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj;
					CableData cableData2 = dataGridViewRow2.Tag as CableData;
					bool flag2 = cableData2 == null;
					if (!flag2)
					{
						this.dataList.Add(cableData2);
					}
				}
			}
		}

		private void buttonImport_Click(object sender, EventArgs e)
		{
			CableDataImportForm cableDataImportForm = new CableDataImportForm();
			bool flag = cableDataImportForm.ShowDialog() != DialogResult.OK;
			if (!flag)
			{
				string excelFile = cableDataImportForm.m_ExcelFile;
				int num = cableDataImportForm.m_StartRow - 1;
				byte[] array = new byte[1];
				array = Encoding.ASCII.GetBytes(cableDataImportForm.m_Attributes[CableField.Field_CableName]);
				int col = (int)(array[0] - 65);
				array = Encoding.ASCII.GetBytes(cableDataImportForm.m_Attributes[CableField.Field_CableModel]);
				int col2 = (int)(array[0] - 65);
				array = Encoding.ASCII.GetBytes(cableDataImportForm.m_Attributes[CableField.Field_CableSpec]);
				int col3 = (int)(array[0] - 65);
				array = Encoding.ASCII.GetBytes(cableDataImportForm.m_Attributes[CableField.Field_SystemType]);
				int col4 = (int)(array[0] - 65);
				array = Encoding.ASCII.GetBytes(cableDataImportForm.m_Attributes[CableField.Field_OutDiameter]);
				int col5 = (int)(array[0] - 65);
				Book book = new XmlBook();
				book.setKey("Beijing YArchitech Tongxing Technology Co.,Ltd", "windows-282b2c0f08cbe70966b4616ea0i8d7n0");
				try
				{
					book.load(excelFile);
				}
				catch (System.Exception ex)
				{
					bool flag2 = "libxl: Permission denied." == ex.Message;
					if (flag2)
					{
						MessageBox.Show("检测到此Excel文件已在外部打开，请先关闭，再重新操作。", "盈建科提示");
						return;
					}
				}
				Sheet sheet = book.getSheet(book.activeSheet());
				for (int i = num; i < sheet.lastRow(); i++)
				{
					CableData data = new CableData();
					data.CableName = CableManageCommon.ReadExcelCell(sheet, i, col);
					data.CableModel = CableManageCommon.ReadExcelCell(sheet, i, col2);
					data.CableSpec = CableManageCommon.ReadExcelCell(sheet, i, col3);
					data.SystemType = CableManageCommon.ReadExcelCell(sheet, i, col4);
					data.OutDiameter = CableManageCommon.ReadExcelCell(sheet, i, col5);
					bool flag3 = data.IsInValid();
					if (!flag3)
					{
						CableData cableData = this.dataList.Find((CableData item) => item.CableModel == data.CableModel && item.CableSpec == data.CableSpec);
						bool flag4 = cableData == null;
						if (flag4)
						{
							this.dataList.Add(data);
							this.IsEdited = true;
						}
						else
						{
							cableData.CableName = data.CableName;
							cableData.SystemType = data.SystemType;
							cableData.OutDiameter = data.OutDiameter;
							this.IsEdited = true;
						}
					}
				}
				this.UpdateDataGridView(true);
			}
		}

		private void buttonDelete_Click(object sender, EventArgs e)
		{
			bool flag = this.dataGridView.SelectedRows.Count <= 0;
			if (!flag)
			{
				int index = this.dataGridView.SelectedRows[0].Index;
				foreach (object obj in this.dataGridView.SelectedRows)
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					CableData cableData = dataGridViewRow.Tag as CableData;
					bool flag2 = cableData == null;
					if (!flag2)
					{
						this.dataList.Remove(cableData);
						this.IsEdited = true;
					}
				}
				this.UpdateDataGridView(true);
				bool flag3 = this.dataGridView.Rows.Count <= 0;
				if (!flag3)
				{
					this.dataGridView.ClearSelection();
					bool flag4 = index >= this.dataGridView.Rows.Count;
					if (flag4)
					{
						this.dataGridView.Rows[0].Selected = true;
					}
					else
					{
						this.dataGridView.Rows[index].Selected = true;
					}
				}
			}
		}

		private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			bool flag = e.RowIndex < 0 || e.RowIndex >= this.dataGridView.Rows.Count || e.ColumnIndex < 1 || e.ColumnIndex >= this.dataGridView.ColumnCount;
			if (flag)
			{
				e.Cancel = true;
			}
			else
			{
				this.m_valueOld = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Trim();
			}
		}

		private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			bool flag = e.RowIndex < 0 || e.RowIndex >= this.dataGridView.Rows.Count || e.ColumnIndex < 1 || e.ColumnIndex >= this.dataGridView.ColumnCount;
			if (!flag)
			{
				string text = string.Empty;
				bool flag2 = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null;
				if (flag2)
				{
					bool flag3 = 2 == e.ColumnIndex || 3 == e.ColumnIndex || 4 == e.ColumnIndex || 5 == e.ColumnIndex;
					if (flag3)
					{
						this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.m_valueOld;
						return;
					}
				}
				else
				{
					text = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
				}
				bool flag4 = text == this.m_valueOld;
				if (!flag4)
				{
					CableData cableData = this.dataGridView.Rows[e.RowIndex].Tag as CableData;
					bool flag5 = cableData == null;
					if (!flag5)
					{
						this.IsEdited = true;
						switch (e.ColumnIndex)
						{
						case 1:
							cableData.CableName = text;
							break;
						case 2:
							cableData.CableModel = text;
							break;
						case 3:
							cableData.CableSpec = text;
							break;
						case 4:
							cableData.SystemType = text;
							break;
						case 5:
							cableData.OutDiameter = text;
							break;
						}
					}
				}
			}
		}

		private void SaveToDatabase()
		{
			this.dataGridView.EndEdit();
			CableDatabase.ThisDb.SaveCableDatas(this.dataList);
			this.IsEdited = false;
		}

		private void CableManagerForm_Resize(object sender, EventArgs e)
		{
		}

		private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			bool flag = this.dataGridView.CurrentCell.OwningColumn.Name == "OutDiameter";
			if (flag)
			{
				e.Control.KeyPress -= this.dataGridView_TextBox_KeyPress;
				e.Control.KeyPress += this.dataGridView_TextBox_KeyPress;
			}
			else
			{
				e.Control.KeyPress -= this.dataGridView_TextBox_KeyPress;
			}
		}

		private void dataGridView_TextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			bool flag = StringTools.IsNumber(e.KeyChar);
			if (!flag)
			{
				e.Handled = true;
			}
		}

		private void dataGridView_Sorted(object sender, EventArgs e)
		{
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				dataGridViewRow.Cells[0].Value = dataGridViewRow.Index + 1;
			}
			this.UpdateDataGridView(false);
		}

		private List<CableData> dataList = null;

		private bool isEdited = false;

		private string m_valueOld = string.Empty;
	}
}
